package com.example.tool.widget.event;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.example.tool.util.DebugLog;

import static com.example.tool.util.DebugLog.EVENT_VIEW;

/**
 * 描述:
 * View.dispatchEvent->View.setOnTouchListener->View.onTouchEvent
 *
 * 在dispatchTouchEvent中会进行OnTouchListener的判断，如果OnTouchListener不为null且返回true，则表示事件被消费，onTouchEvent不会被执行；否则执行onTouchEvent。
 * @author: liangkun
 * @date: 2019/11/22 11:35
 */
public class MyButton extends android.support.v7.widget.AppCompatButton {
    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                DebugLog.d(EVENT_VIEW + "MyButton onTouchEvent ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                DebugLog.d(EVENT_VIEW + "MyButton onTouchEvent ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                DebugLog.d(EVENT_VIEW + "MyButton onTouchEvent ACTION_UP");
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        //如果父控件move 拦截了，子控件申请不要拦截
//        getParent().requestDisallowInterceptTouchEvent(true);
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                DebugLog.d(EVENT_VIEW + "MyButton dispatchTouchEvent ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                DebugLog.d(EVENT_VIEW + "MyButton dispatchTouchEvent ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                DebugLog.d(EVENT_VIEW + "MyButton  dispatchTouchEvent ACTION_UP");
                break;

            default:
                break;
        }
        return super.dispatchTouchEvent(event);
    }

}
